package com.sam4cloud.oauth2.project.controller;

import cn.hutool.json.JSONUtil;
import com.sam4cloud.common.core.model.R;
import com.sam4cloud.common.core.utils.IpUtils;
import com.sam4cloud.oauth2.project.model.dto.LoginDTO;
import com.sam4cloud.oauth2.project.model.vo.AccessTokenVO;
import com.sam4cloud.oauth2.project.model.vo.LoginUser;
import com.sam4cloud.oauth2.project.service.IOauthAccountService;
import com.sam4cloud.oauth2.project.service.IOauthUserService;
import com.sam4cloud.oauth2.utils.CookieUtils;
import com.sam4cloud.oauth2.utils.EncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Api(tags = "认证中心")
@RestController("/sso")
public class SsoController {

    @Autowired
    private IOauthUserService oauthUserService;

    @Autowired
    private IOauthAccountService oauthAccountService;

    //登录成功后重定向地址
    @PostMapping("/loginSuccess")
    public R<String> loginSuccess() {
        return R.ok("登录成功");
    }


    @ApiOperation("PC页面登录接口")
    @PostMapping("/login")
    public R<String> login(@RequestBody @Validated(LoginDTO.PCLogin.class) LoginDTO loginDTO,
                           HttpServletRequest request, HttpServletResponse response) {
        //登录检测
        LoginUser loginUser = oauthUserService.checkLogin(loginDTO);
        //设置cookie 实现单点登录 127.0.0.1
        CookieUtils.addCookie(response, "SESSION_USER", EncryptUtils.aesEncrypt(JSONUtil.toJsonStr(loginUser)), request.getServerName());
        return R.ok("登录成功");
    }


    @ApiOperation("获取accessToken")
    @GetMapping("/token")
    public R<AccessTokenVO> authorize(@RequestParam("appId") String appId,
                                      @RequestParam("appSecret") String appSecret,
                                      HttpServletRequest request) {
        //请求的ip
        String ipAddr = IpUtils.getClientIpAddr(request);
        //生成一次性临时code码
        AccessTokenVO accessTokenVO = oauthAccountService.createAccessToken(appId, appSecret, ipAddr);
        //记录授权日志
        return R.ok(accessTokenVO);
    }


    @ApiOperation("链接授权码登录 返回code码")
    @GetMapping("/authorize")
    public void authorize(@RequestParam("appId") String appId,
                          @RequestParam("redirectUrl") String redirectUrl,
                          HttpServletRequest request, HttpServletResponse response) throws IOException {
        //请求的ip
        String reqIp = IpUtils.getClientIpAddr(request);
        //生成一次性临时code码
        String code = oauthAccountService.createTempCode(appId, reqIp, redirectUrl);
        //todo 记录授权日志
        //重定向回去附带code码
        response.sendRedirect(redirectUrl + "?code=" + code);

    }

    @ApiOperation(value = "置换用户信息", notes = "使用临时code置换用户信息")
    @GetMapping("/userInfo")
    public R<AccessTokenVO> userInfo(@RequestParam("accessToken") String accessToken,
                                     @RequestParam("code") String code) {
        AccessTokenVO accessTokenVO = oauthAccountService.getUserInfo(accessToken, code);
        return R.ok(accessTokenVO);
    }
}